import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/homeworks.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/grids/homeworkRecordsDataGrid.dart';
import '../../themes/appTheme.dart';

class MyHomeworkRecords extends StatefulWidget {
  static const String routeName = '/student/report/homework';

  @override
  State<MyHomeworkRecords> createState() => _MyHomeworkRecordsState();
}

class _MyHomeworkRecordsState extends State<MyHomeworkRecords> {
  final box = Hive.box('appconfig');
  final HomeworksController controller = Get.find();

  @override
  void initState() {
    bool isStudent = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Student;
    String _classId = box.get(CacheManagerKey['currentclass'].toString());

    if (isStudent) {
      controller.assignmentRecords.clear();

      Future.delayed(Duration(milliseconds: 200), (() {
        controller.fetchStudentHomeworkRecordsByClass(_classId);
      }));
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build student Homework Records');

    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Container(
        decoration: BoxDecoration(
          color: Get.isDarkMode
              ? AppTheme.colors.darkTheme2
              : Colors.white.withOpacity(0.3),
          borderRadius: BorderRadius.circular(3.0),
        ),
        child: Column(
          children: [
            Expanded(
              child: controller.obx(
                (data) => HomeworkRecordsDataGrid(controller.assignmentRecords),
                onEmpty: HomeworkRecordsDataGrid(controller.assignmentRecords),
                onLoading: Container(),
                // onLoading: Center(
                //     child: CircularProgressIndicator(
                //   backgroundColor: Get.theme.primaryColorDark,
                //   strokeWidth: 9.0,
                // )),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
